package pers.lipeng.greedy_snake;

import javax.swing.*;
import java.awt.*;

/**
 * @author APengG
 */
public class Food extends JLabel {
    /**
     * 食物坐标
     */
    private int bodyX;
    private int bodyY;

    public Food(int x,int y){
        this.bodyX = x;
        this.bodyY = y;
        //设置食物大小
        this.setSize(new Dimension(15,15));
        //设置背景颜色为红色
//        this.setBackground(Color.RED);
        //设置食物不透明
//        this.setOpaque(true);

        //设置背景图片,通过以下步骤才可以改变图片的大小
        ImageIcon imageIcon = new ImageIcon(getClass().getResource("/snake/img/food.png"));
        //设置图片大小
        Image image = imageIcon.getImage();
        Image scaledInstance = image.getScaledInstance(15, 15, Image.SCALE_DEFAULT);
        imageIcon.setImage(scaledInstance);
        this.setIcon(imageIcon);
    }

    public Food loadFood(int x,int y){
        //食物我们可以复用只需要改变坐标即可
        this.bodyX = x;
        this.bodyY = y;
        return this;
    }

    public int getBodyX() {
        return bodyX;
    }

    public void setBodyX(int bodyX) {
        this.bodyX = bodyX;
    }

    public int getBodyY() {
        return bodyY;
    }

    public void setBodyY(int bodyY) {
        this.bodyY = bodyY;
    }
}
